﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace TJDevHouse.BackupUtils.CopyModifiedDirs
{
	class MainEntry
	{
		/// <summary>
		/// The TraceSource to use for tracing
		/// </summary>
		public static TraceSource traceSource = new TraceSource("TJDevHouse.BackupUtils.CopyModifiedDirs");



		static int Main(string[] args)
		{


			try
			{
#if DEBUG
				args = new string[3];
				args[0] = "e:\\projects\\";
				args[1] = "i:\\projects";
				args[2] = ".hg";
#endif

				traceSource.TraceEvent(TraceEventType.Information, 1, "CopyModifiedDirs started with {0} arguments", args.Length);
				traceSource.TraceData(TraceEventType.Verbose, 2, args);

				if (args.Length != 3)
				{
					traceSource.TraceEvent(TraceEventType.Verbose, 4, "Not enough arguments passed to continue");
					OutputHelp();
					return Exit(1);
				}

				DirectoryInfo source = new DirectoryInfo(args[0]);
				DirectoryInfo target = new DirectoryInfo(args[1]);
				string dirname = args[2];

				ModifiedDirectoryCopier copier = new ModifiedDirectoryCopier(source, target, dirname);

				int result = copier.PerformCopy();

				traceSource.TraceEvent(TraceEventType.Information, 16, "Copied {0} directories during operation", result);

				return Exit(0);
			}
			catch (Exception e)
			{
				traceSource.TraceEvent(TraceEventType.Critical, 5, "A fatal error occured: {0}", e.ToString());
				return Exit(1);
			}
		}

		/// <summary>
		/// Displays the help message.
		/// </summary>
		private static void OutputHelp()
		{
			string message = @"
I am a command line tool designed to help with backing up directories, specificaly local mercurial repositories.

Given a source directory, a target directory and a directory name to search for, I will copy all directories matching the given name to the target directory.

I keep the directory structure of the source directory when copying to the target directory. For example:-

	The following directory tree exists:
		c:\projects\my_project\foo\bar\wibble\wobble

	I am then executed with the following arguments:
		c:\projects d:\backup\ foo

	I will copy the 'foo' directory and it's contents to:
		d:\backup\my_project\foo\bar\wibble\wobble

I will check whether the source directory has already been copied, and then only copy it if the source is newer than the target.

My syntax:
	CopyModifiedDirs source target dirname

e.g.
	To copy all mercurial .hg directories within e:\projects to i:\projects_backup :-
	CopyModifiedDirs e:\projects\ i:\projects_backup\ .hg

";
			Console.WriteLine(message);
		}

		/// <summary>
		/// Performs all exit logic.
		/// </summary>
		private static int Exit(int returnCode)
		{
			if (returnCode != 0 && returnCode != 1)
			{
				throw new ArgumentOutOfRangeException("Return code must be either 0 or 1");
			}

			traceSource.TraceEvent(TraceEventType.Verbose, 3, "Exiting with code {0}", returnCode);

#if DEBUG
			Console.ReadKey();
#endif
			return returnCode;
		}
	}
}
